package com.zj.user.controller;

import com.zj.user.Repository.UserRepository;
import com.zj.user.component.UserRoleAuthentication;
import com.zj.user.domain.User;
import com.zj.user.enums.RoleEnum;
import com.zj.user.enums.UserEnum;
import com.zj.user.service.UserService;
import com.zj.user.utils.ListPagingUtils;
import com.zj.user.utils.ResultVoUtil;
import com.zj.user.vo.ResultVo;
import com.zj.user.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Auther: zj
 * @Date: 2018/11/13 12:29
 * @Description:
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    UserRoleAuthentication userRoleAuthentication;

    /**
     *  获取用户的信息，需用户权限
     * @param request request
     * @return userInfo
     */
    @GetMapping("/getPersonalInfo")
    public ResultVo getPersonalInfo(HttpServletRequest request) {
        String username = userRoleAuthentication.getUsernameAndAutenticateUserRoleFromRequest( request, RoleEnum.User.getMessage() );
        if (Objects.equals( username, "false" )) {
            return ResultVoUtil.error( UserEnum.USERROLEERROR);
        }
        User user = userService.findOneUserByUsername( username );
        return ResultVoUtil.success( user );
    }

    /**
     *  获取用户的信息，需管理员权限
     * @param request request
     * @return userInfo
     */
    @GetMapping("/getPersonalInfo_admin")
    public ResultVo getPersonalInfoAdmin(HttpServletRequest request) {
        String username = userRoleAuthentication.getUsernameAndAutenticateUserRoleFromRequest( request, RoleEnum.Admin.getMessage() );
        if (Objects.equals( username, "false" )) {
            return ResultVoUtil.error( UserEnum.USERROLEERROR);
        }
        User user = userService.findOneUserByUsername( username );
        return ResultVoUtil.success( user );
    }


    /**
     * 获取所以普通用户列表，分页，管理员权限
     * @param request request
     * @param pageIndex 当前页
     * @return list
     */
    @GetMapping("/getAllUserList")
    public ResultVo getUserList(HttpServletRequest request,
                                @RequestParam("pageIndex") Integer pageIndex) {
        String username = userRoleAuthentication.getUsernameAndAutenticateUserRoleFromRequest( request, RoleEnum.Admin.getMessage() );
        if (Objects.equals( username, "false" )) {
            return ResultVoUtil.error( UserEnum.USERROLEERROR);
        }
        List<User> lists = userService.selectAllUser();
        lists.removeIf( user -> Objects.equals( user.getRole(), RoleEnum.Admin.getMessage() ) );//删除管理员账号
        List<Object> list =ListPagingUtils.getPaging( lists,pageIndex-1,5 );
        return ResultVoUtil.success(list);
    }

    /**
     * 获取所以普通用户列表的size,
     * @param request request
     * @return size
     */
    @GetMapping("/getAllUserListTotal")
    public ResultVo getAllUserListTotal(HttpServletRequest request) {
        String username = userRoleAuthentication.getUsernameAndAutenticateUserRoleFromRequest( request, RoleEnum.Admin.getMessage() );
        if (Objects.equals( username, "false" )) {
            return ResultVoUtil.error( UserEnum.USERROLEERROR);
        }
        List<User> lists = userService.selectAllUser();
        lists.removeIf( user -> Objects.equals( user.getRole(), RoleEnum.Admin.getMessage() ) );//删除管理员账号

        return ResultVoUtil.success(lists.size());
    }

    /**
     * 删除 用户
     * @param request
     * @param userId
     * @return
     */
    @GetMapping("/admin/deleteUser")
    public ResultVo adminDeleteUser(HttpServletRequest request,
                                    @RequestParam("userId") Integer userId) {
        String username = userRoleAuthentication.getUsernameAndAutenticateUserRoleFromRequest( request, RoleEnum.Admin.getMessage() );
        if (Objects.equals( username, "false" )) {
            return ResultVoUtil.error( UserEnum.USERROLEERROR);
        }

        try {
            userService.deleteUserByUserId( userId );
            return ResultVoUtil.success();
        }catch (Exception e) {
            return ResultVoUtil.error( "出现异常" );
        }
    }

    @Autowired
    UserRepository userRepository;
    /**
     * 切换用户的状态，管理员权限
     * @param request request
     * @param userId user id
     * @param hasFreeze 冻结标志
     * @return 成功
     */
    @GetMapping("/admin/changeUserStatus")
    public ResultVo changeUserStatus(HttpServletRequest request,
                                     @RequestParam("userId") Integer userId,
                                     @RequestParam("hasFreeze") String hasFreeze) {
        String username = userRoleAuthentication.getUsernameAndAutenticateUserRoleFromRequest( request, RoleEnum.Admin.getMessage() );
        if (Objects.equals( username, "false" )) {
            return ResultVoUtil.error( UserEnum.USERROLEERROR);
        }

        System.out.println( hasFreeze );

        User user =userService.findOneUserById( userId );

        if (user==null) {
            return ResultVoUtil.error( "userId有误" );
        }
        if (Objects.equals( hasFreeze, "正常" )) {
            user.setHasFreeze( 1 );
            userService.UpDate( user );
        }
        if (Objects.equals( hasFreeze,"冻结" )) {
            user.setHasFreeze( 0 );
            userService.UpDate( user );
        }
        return ResultVoUtil.success(UserEnum.USER_CHANGE_STATUS_SUCCESS.getMessage());
    }


    @GetMapping("/test1")
    public ResultVo test1() {
        User user = userService.findOneTest( 1 );
        UserVo userVo= new UserVo();
        /**
        userVo.setId( user.getId() );
        userVo.setUsername( user.getUsername() );
        userVo.setPassword( user.getPassword() );
        userVo.setEmail( user.getEmail() );
        userVo.setHasFreeze( user.getHasFreeze() );
        userVo.setRole( user.getRole() );
        userVo.setCreateTime( user.getCreateTime() );
         **/
        //上面一种方法，太麻烦了，换一种
        BeanUtils.copyProperties( user,userVo );

        return ResultVoUtil.success( userVo );
    }

}
